import axios from './init'
import { addUserObj } from '@/store/user/userIntetFace'

/**
 * @function
 * @param params
 * @returns
 */

export const getAllUser = async (params: {
  pagenum: number
  query?: string
  pagesize: number
}) => {
  if (!params.pagenum || !params.pagesize) return new Error('参数错误')
  const { data: res } = await axios.get('users', { params })

  return res
}

/**
 * @function
 * @param params
 * @returns
 */

export const addUser = async (params: addUserObj) => {
  const { data: res } = await axios.post('users', params)
  return res
}

/**
 * @function
 * @param params
 * @returns
 */

export const deleteUser = async (id:number) => {
  const { data: res } = await axios.delete(`users/${id}`)
  return res
}

/**
 *
 *
 * */

export const modifyUser = async (id:number, params:{ email:string, mobile:number}) => {
  const { data: res } = await axios.put(`users/${id}`, { ...params })
  return res
}

export const updateState = async (id:number, state:boolean) => {
  const { data: res } = await axios.put(`users/${id}/state/${state}`)
  return res
}

export const getAllRoles = async () => axios.get('roles')

export const updateUserRole = async (id:number, rid:string) => {
  const { data: res } = await axios.put(`users/${id}/role`, { rid })

  return res
}
